package io.dbw.modules.business.controller.sys;

import io.dbw.common.utils.*;
import io.dbw.modules.business.dto.BooksDTO;
import io.dbw.modules.business.entity.SysBooksEntity;
import io.dbw.modules.business.service.SysBooksService;
import io.dbw.modules.business.vo.HomeVO;
import io.dbw.modules.utils.MessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.Map;



/**
 *
 * @author lxc
 * @date 2024-03-30 16:55:49
 */
@Validated
@RestController
@RequestMapping("sys/manage")
@Api(tags = "图书管理")
public class SysBooksController extends AbstractController {
    @Autowired
    private SysBooksService sysBooksService;
    @Autowired
    private MessageUtils messageUtils;

    @GetMapping("/statistics")
    @ApiOperation(value="首页统计")
    public Result<HomeVO>  statistics(){
        return writeSuccess(sysBooksService.queryBooksStatistics());
    }

    @GetMapping("/test")
    @ApiOperation(value="测试国际化")
    public Result<HomeVO>  test(){
        return writeSuccess(messageUtils.getMessage("addSuccess"));
    }

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation(value="列表查询")
    @RequiresPermissions("manage:books:list")
    public Result<PageUtils<SysBooksEntity>> list(@RequestBody BooksDTO booksDTO){
        Map<String, Object> params = MapToDTOUtil.dtoTOMap(booksDTO);
        params.put("userId",1);
        PageUtils page = sysBooksService.queryPage(params);
        return writeSuccess(page);
    }


    /**
     * 信息
     */
    @ApiOperation("根据ID查询信息")
    @GetMapping("/info/{bookId}")
    @RequiresPermissions("manage:books:info")
    @ApiImplicitParam(paramType = "path",name = "bookId",value ="ID",dataType ="Long",required = true)
    public Result info(@PathVariable("bookId") Long bookId){
		SysBooksEntity sysBooks = sysBooksService.getById(bookId);
        return writeSuccess(sysBooks);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("保存")
    @RequiresPermissions("manage:books:save")
    public Result save(@Validated @RequestBody SysBooksEntity sysBooks){
        sysBooks.setCreateBy(getUserId());
        sysBooks.setUpdateBy(getUserId());
        sysBooks.setDeleteFlag(Constant.SYSTEM_COMMON_N);
		sysBooksService.save(sysBooks);
        return writeSuccess();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value="修改")
    @RequiresPermissions("manage:books:update")
    public Result update(@Validated @RequestBody SysBooksEntity sysBooks){
        AssertUtils.isNull(sysBooks.getBookId(),"图书ID不能为空");
        sysBooks.setUpdateBy(getUserId());
		sysBooksService.updateById(sysBooks);
        return writeSuccess();
    }
    /**
     * 删除
     */
    @GetMapping("/delete/{bookId}")
    @ApiOperation(value="删除")
    @RequiresPermissions("manage:books:delete")
    @ApiImplicitParam(paramType = "path",name = "bookId",value ="id",dataType ="Long",required = true)
    public Result delete(@PathVariable("bookId") Long bookId){
        sysBooksService.removeById(bookId);
        return writeSuccess();
    }




}
